package com.sidneysimmons.financially.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.fragments.AccountFragment;
import com.sidneysimmons.financially.managers.AccountManager;

public class AccountPagerAdapter extends FragmentStatePagerAdapter {
	
	private Context context;
	private ArrayList<Account> accounts = new ArrayList<Account>();
	
	public AccountPagerAdapter(FragmentManager fragmentManager, Context context) {
		super(fragmentManager);
		this.context = context;
		
		// Get accounts from database
		AccountManager accountManager = new AccountManager(context);
		accounts = accountManager.getAccounts();
	}

	@Override
	public Fragment getItem(int arg0) {
		return AccountFragment.newInstance(accounts.get(arg0));
	}

	@Override
	public int getCount() {
		return accounts.size();
	}
	
	@Override
	public int getItemPosition (Object object) {
		return PagerAdapter.POSITION_NONE;
	}
	
	@Override
	public void notifyDataSetChanged() {
		// refresh accounts from database
		AccountManager accountManager = new AccountManager(context);
		accounts = accountManager.getAccounts();
		
		super.notifyDataSetChanged();
	}
}